{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Decode the Message"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: decodeMessage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #解密消息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你字符串 <code>key</code> 和 <code>message</code> ，分别表示一个加密密钥和一段加密消息。解密 <code>message</code> 的步骤如下：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>使用 <code>key</code> 中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 <strong>顺序</strong> 。</li>\n",
    "\t<li>将替换表与普通英文字母表对齐，形成对照表。</li>\n",
    "\t<li>按照对照表 <strong>替换</strong> <code>message</code> 中的每个字母。</li>\n",
    "\t<li>空格 <code>' '</code> 保持不变。</li>\n",
    "</ol>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，<code>key = \"<em><strong>hap</strong></em>p<em><strong>y</strong></em> <em><strong>bo</strong></em>y\"</code>（实际的加密密钥会包含字母表中每个字母 <strong>至少一次</strong>），据此，可以得到部分对照表（<code>'h' -&gt; 'a'</code>、<code>'a' -&gt; 'b'</code>、<code>'p' -&gt; 'c'</code>、<code>'y' -&gt; 'd'</code>、<code>'b' -&gt; 'e'</code>、<code>'o' -&gt; 'f'</code>）。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回解密后的消息。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/05/08/ex1new4.jpg\" style=\"width: 752px; height: 150px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>key = \"the quick brown fox jumps over the lazy dog\", message = \"vkbs bs t suepuv\"\n",
    "<strong>输出：</strong>\"this is a secret\"\n",
    "<strong>解释：</strong>对照表如上图所示。\n",
    "提取 \"<em><strong>the</strong></em> <em><strong>quick</strong></em> <em><strong>brown</strong></em> <em><strong>f</strong></em>o<em><strong>x</strong></em> <em><strong>j</strong></em>u<em><strong>mps</strong></em> o<em><strong>v</strong></em>er the <em><strong>lazy</strong></em> <em><strong>d</strong></em>o<em><strong>g</strong></em>\" 中每个字母的首次出现可以得到替换表。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/05/08/ex2new.jpg\" style=\"width: 754px; height: 150px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>key = \"eljuxhpwnyrdgtqkviszcfmabo\", message = \"zwx hnfx lqantp mnoeius ycgk vcnjrdb\"\n",
    "<strong>输出：</strong>\"the five boxing wizards jump quickly\"\n",
    "<strong>解释：</strong>对照表如上图所示。\n",
    "提取 \"<em><strong>eljuxhpwnyrdgtqkviszcfmabo</strong></em>\" 中每个字母的首次出现可以得到替换表。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>26 &lt;= key.length &lt;= 2000</code></li>\n",
    "\t<li><code>key</code> 由小写英文字母及 <code>' '</code> 组成</li>\n",
    "\t<li><code>key</code> 包含英文字母表中每个字符（<code>'a'</code> 到 <code>'z'</code>）<strong>至少一次</strong></li>\n",
    "\t<li><code>1 &lt;= message.length &lt;= 2000</code></li>\n",
    "\t<li><code>message</code> 由小写英文字母和 <code>' '</code> 组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [decode-the-message](https://leetcode.cn/problems/decode-the-message/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [decode-the-message](https://leetcode.cn/problems/decode-the-message/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"the quick brown fox jumps over the lazy dog\"\\n\"vkbs bs t suepuv\"', '\"eljuxhpwnyrdgtqkviszcfmabo\"\\n\"zwx hnfx lqantp mnoeius ycgk vcnjrdb\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        pwd_map = {ord(' '): ord(' ')}\n",
    "        start = 97\n",
    "\n",
    "        for char in key:\n",
    "            if ord(char) not in pwd_map:\n",
    "                pwd_map[ord(char)] = start\n",
    "                start += 1\n",
    "        return message.translate(pwd_map)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        pwd_map = {}\n",
    "        start = 97\n",
    "\n",
    "        for char in key:\n",
    "            if ord(char) not in pwd_map and char!=' ':\n",
    "                pwd_map[ord(char)] = start\n",
    "                start += 1\n",
    "        return message.translate(pwd_map)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        rules = dict()\n",
    "        cur = \"a\"\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        result = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        idx = ord(\"a\")\n",
    "        mapping = dict()\n",
    "        mapping[\" \"] = \" \"\n",
    "        for k in key:\n",
    "            if k not in mapping:\n",
    "                mapping[k] = chr(idx)\n",
    "                idx += 1\n",
    "        assert len(mapping) == 27\n",
    "        return \"\".join([mapping[s] for s in message])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        i = 97\n",
    "        ma = {' ': ' '}\n",
    "        key = key.replace(' ','')\n",
    "        for k in key:\n",
    "\n",
    "            if k not in ma.keys():\n",
    "                ma[k] = chr(i)\n",
    "                i += 1\n",
    "            else:\n",
    "                continue\n",
    "        # print(ma)\n",
    "        ans = ''\n",
    "        for s in message:\n",
    "\n",
    "            v = ma[s]\n",
    "            ans += v\n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        letters = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        d = {}\n",
    "        a = 0\n",
    "        for k in key:\n",
    "            if k != ' ' and k not in d.keys():\n",
    "                d[k] = letters[a]\n",
    "                a += 1\n",
    "        n = ''\n",
    "        for m in message:\n",
    "            if m != ' ':\n",
    "                n += d[m]\n",
    "            else:\n",
    "                n += ' '\n",
    "        return n\n",
    "             \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d = {\" \": \" \"}\n",
    "        i=0\n",
    "        for c in key:\n",
    "            if c not in d:\n",
    "                d[c] = ascii_lowercase[i]\n",
    "                i+=1\n",
    "        return \"\".join(d[c] for c in message)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hash_d = {' ':' '}\n",
    "        cnt = 0\n",
    "        res = ''\n",
    "        for i in key:\n",
    "            if i not in hash_d:\n",
    "                hash_d[i] = chr(ord('a') + cnt)\n",
    "                cnt += 1\n",
    "        for i in message:\n",
    "            res += hash_d[i]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        cur = \"a\"\n",
    "        rules = dict()\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        ans = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        mapVal = {}\n",
    "        iter = 0\n",
    "        for c in key:\n",
    "            if c not in mapVal and c!=\" \":\n",
    "                mapVal[c] = iter\n",
    "                iter += 1\n",
    "        print(mapVal)\n",
    "        ans = \"\"\n",
    "        for c in message:\n",
    "            if c == \" \":\n",
    "                ans += \" \"\n",
    "            else:\n",
    "                ans+=chr(mapVal[c]+97)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hash_d = {' ':' '}\n",
    "        cnt = 0\n",
    "        res = ''\n",
    "        for i in key:\n",
    "            if i not in hash_d:\n",
    "                hash_d[i] = chr(ord('a') + cnt)\n",
    "                cnt += 1\n",
    "        for i in message:\n",
    "            res += hash_d[i]\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        pwd_map = {}\n",
    "        start = 97\n",
    "\n",
    "        for char in key:\n",
    "            if ord(char) not in pwd_map and char!=' ':\n",
    "                pwd_map[ord(char)] = start\n",
    "                start += 1\n",
    "        return message.translate(pwd_map)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        table = {}\n",
    "        table[' '] = ' '\n",
    "        i = 0\n",
    "        for x in key:\n",
    "            if x in table:\n",
    "                continue\n",
    "            table[x] = chr(ord('a') + i)\n",
    "            i += 1\n",
    "        return ''.join([table[_] for _ in message])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic = {\" \": \" \"}\n",
    "        ans = \"\"\n",
    "        i = 0\n",
    "        for c in key:\n",
    "            if c not in dic:\n",
    "                dic[c] = chr(ord(\"a\") + i)\n",
    "                i += 1\n",
    "        for c in message:\n",
    "            ans += dic[c]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        tmp={}\n",
    "        length=0\n",
    "        for i in key:\n",
    "            if i not in tmp and i!=\" \":\n",
    "                tmp[i]=length+97\n",
    "                length+=1\n",
    "\n",
    "        res=\"\"\n",
    "        for i in message:\n",
    "            if i!=\" \":\n",
    "                res+=(chr(tmp[i]))\n",
    "            else:\n",
    "                res+=i\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic = {}\n",
    "        length1 = len(key)\n",
    "        length2 = len(message)\n",
    "        count = 0\n",
    "        for i in range(length1):\n",
    "            if key[i] != ' ' and key[i] not in dic:\n",
    "                dic[key[i]] = chr(97 + count)\n",
    "                count = count + 1\n",
    "        \n",
    "        dic[' '] = ' '\n",
    "        num = \"\"\n",
    "        for i in range(length2):\n",
    "            num += dic[message[i]]\n",
    "        \n",
    "        return num\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d = {\" \": \" \"}\n",
    "        i = 0\n",
    "        for c in key:\n",
    "            if c not in d:\n",
    "                d[c] = ascii_lowercase[i]\n",
    "                i += 1\n",
    "        return \"\".join(d[c] for c in message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        output_list = []\n",
    "        \n",
    "        newkey=''\n",
    "        for char in key:\n",
    "            if char not in newkey:\n",
    "                newkey+=char\n",
    "        \n",
    "        qq=\"\".join(newkey.split(\" \"))\n",
    "        newqq=list(qq)\n",
    "        value=[\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]\n",
    "        dict1=dict(zip(newqq,value))\n",
    "        \n",
    "        \n",
    "        res=\"\"\n",
    "        for i in message:\n",
    "            res+=\" \" if i==\" \" else dict1[i]\n",
    "        return res\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        cur = \"a\"\n",
    "        rules = dict()\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        ans = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dc = dict()\n",
    "        all_alpha_1 = [chr(i) for i in range(ord('a'), ord('z') + 1)]\n",
    "        for k in key:\n",
    "            if k.isalpha():\n",
    "                dc.setdefault(k, None)\n",
    "        for k in dc:\n",
    "            dc[k] = all_alpha_1.pop(0)\n",
    "        out = ''.join([dc.get(i, ' ') for i in message])\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hash_d = {' ':' '}\n",
    "        cnt = 0\n",
    "        res = ''\n",
    "        for i in key:\n",
    "            if i not in hash_d:\n",
    "                hash_d[i] = chr(ord('a') + cnt)\n",
    "                cnt += 1\n",
    "        for i in message:\n",
    "            res += hash_d[i]\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hashmap = {}\n",
    "        index = 0\n",
    "        for k in key:\n",
    "            if k != ' ' and (k not in hashmap):\n",
    "                hashmap[k] = chr(ord('a')+index)\n",
    "                index += 1\n",
    "        result = []\n",
    "        for c in message:\n",
    "            if c == ' ':\n",
    "                result.append(c)\n",
    "            else:\n",
    "                result.append(hashmap[c])\n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hashMap = dict()\n",
    "        idx = 0\n",
    "        for ch in key:\n",
    "            if ch == ' ' or ch in hashMap:\n",
    "                continue\n",
    "            hashMap[ch] = chr(ord('a') + idx)\n",
    "            idx += 1\n",
    "        \n",
    "        return \"\".join([ hashMap[ch] if ch != ' ' else ' ' for ch in message ])\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        cur = \"a\"\n",
    "        rules = dict()\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        ans = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d={}\n",
    "        i=97\n",
    "        for j in range(len(key)):\n",
    "            if key[j] not in d.values() and not key[j].isspace():\n",
    "                d[chr(i)]=key[j]\n",
    "                i+=1\n",
    "        #print(d)\n",
    "        dt={v:k for k,v in d.items()}\n",
    "        ot=\"\"\n",
    "        for i in message:\n",
    "            if i.isspace():\n",
    "                ot+=i\n",
    "            else:\n",
    "                ot+=dt[i]\n",
    "        return ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        keys = {}\n",
    "        start = 97\n",
    "        index = 0\n",
    "        for c in key:\n",
    "            if c == ' ':\n",
    "                continue\n",
    "            if c not in keys:\n",
    "                keys[c] = chr(start + index) \n",
    "                index += 1\n",
    "        \n",
    "        result = \"\"\n",
    "        for i in range(len(message)):\n",
    "            m = message[i]\n",
    "            if m == ' ':\n",
    "                result += m\n",
    "                continue\n",
    "            result += keys[m]\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        key = key.replace(' ','')\n",
    "        dic = {' ':0}\n",
    "        v = 1\n",
    "        ans =''\n",
    "        for k in key:\n",
    "            if k not in dic:\n",
    "                dic[k] = v\n",
    "                v +=1\n",
    "        lst = [' ']\n",
    "        for l in range(ord('a'),ord('z')+1):\n",
    "            lst += chr(l)\n",
    "        \n",
    "        for i in message:\n",
    "            ans += lst[dic[i]]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d={}\n",
    "        i=97\n",
    "        j=0\n",
    "        while j<len(key):\n",
    "            if key[j] not in d.values() and not key[j].isspace():\n",
    "                d[chr(i)]=key[j]\n",
    "                i+=1\n",
    "                j+=1\n",
    "            else:\n",
    "                j+=1\n",
    "        #print(d)\n",
    "        dt={v:k for k,v in d.items()}\n",
    "        ot=\"\"\n",
    "        for i in message:\n",
    "            if i.isspace():\n",
    "                ot+=i\n",
    "            else:\n",
    "                ot+=dt[i]\n",
    "        return ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        map=dict()\n",
    "        cur=\"a\"\n",
    "        for i in key:\n",
    "            if i!=\" \" and i not in map:\n",
    "                map[i]=cur\n",
    "                cur=chr(ord(cur)+1)\n",
    "        ans=\"\".join(map.get(i,\" \") for i in message)\n",
    "        return ans    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        i = 97\n",
    "        ma = {' ': ' '}\n",
    "        key = key.replace(' ','')\n",
    "        for k in key:\n",
    "\n",
    "            if k not in ma.keys():\n",
    "                ma[k] = chr(i)\n",
    "                i += 1\n",
    "            else:\n",
    "                continue\n",
    "        # print(ma)\n",
    "        ans = ''\n",
    "        for s in message:\n",
    "\n",
    "            v = ma[s]\n",
    "            ans += v\n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        tag = {\" \": \" \"}\n",
    "        stand = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "        stand_index = 0\n",
    "        for k in key:\n",
    "            if k not in tag:\n",
    "                tag[k] = stand[stand_index]\n",
    "                stand_index += 1\n",
    "        ans = \"\"\n",
    "        for m in message:\n",
    "            ans += tag[m]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic = {\" \": \" \"}\n",
    "        i = 0\n",
    "        for c in key:\n",
    "            if c not in dic:\n",
    "                dic[c] = chr(ord(\"a\") + i)\n",
    "                i += 1\n",
    "        return \"\".join([dic[c] for c in message])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hash_d = {' ':' '}\n",
    "        cnt = 0\n",
    "        res = ''\n",
    "        for i in key:\n",
    "            if i not in hash_d:\n",
    "                hash_d[i] = chr(ord('a') + cnt)\n",
    "                cnt += 1\n",
    "        for i in message:\n",
    "            res += hash_d[i]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        mp = {' ':' '} #提前给空格mapping,减少判断\n",
    "        j=0\n",
    "        for i in key:\n",
    "            if i not in mp:\n",
    "                mp[i]=chr(ord('a')+j)\n",
    "                j+=1\n",
    "        return ''.join(mp[c] for c in message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic = {}\n",
    "        i = 0\n",
    "        for c in key:\n",
    "            if c == \" \":\n",
    "                continue\n",
    "            if c not in dic:\n",
    "                dic[c] = i\n",
    "                i += 1\n",
    "        ans = \"\"\n",
    "        for c in message:\n",
    "            if c == \" \":\n",
    "                ans += c\n",
    "            else:\n",
    "                ans += chr(dic[c] + 97)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d = {}\n",
    "        i = 0\n",
    "        j = 0\n",
    "        while len(d) != 26 and i + j != len(key):\n",
    "            if key[i + j] != \" \":\n",
    "                if key[i + j] not in d.keys():\n",
    "                    d[key[i + j]] = chr(i + 97)\n",
    "                    i += 1\n",
    "                else:\n",
    "                    j += 1\n",
    "            else:\n",
    "                j += 1\n",
    "        l = ''\n",
    "        for i in message:\n",
    "            if i != \" \":\n",
    "                l += d[i]\n",
    "            else:\n",
    "                l += i\n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        cur = \"a\"\n",
    "        rules = dict()\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        ans = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        pwtable = dict()\n",
    "        alphabet = [chr(i) for i in range(ord(\"a\"),ord(\"z\")+1)]\n",
    "        n = 0\n",
    "        print(alphabet)\n",
    "        for i in key:\n",
    "            if i != \" \" and i not in pwtable.keys():\n",
    "                print([i])\n",
    "                pwtable[i] = alphabet[n]\n",
    "                n += 1\n",
    "        messagestr = \"\"\n",
    "        for i in message:\n",
    "            if i == \" \":\n",
    "                messagestr = messagestr + i\n",
    "            elif i in pwtable.keys():\n",
    "                messagestr = messagestr + pwtable[i]\n",
    "        return messagestr\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        mp = {' ': ' '}\n",
    "        i = 0\n",
    "        for c in key:\n",
    "            if c not in mp:\n",
    "                mp[c] = ascii_lowercase[i]\n",
    "                i += 1\n",
    "        return ''.join(mp[c] for c in message)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        key=key.split()\n",
    "        key2=key3=\"\"\n",
    "        for i in key:\n",
    "            key2+=i\n",
    "        for i in key2:\n",
    "            if i not in key3:\n",
    "                key3+=i   \n",
    "        def func1(a:str)->str:\n",
    "            if a==\" \":\n",
    "                return \" \"\n",
    "            for i in range(26):\n",
    "                if ord(a)==ord(key3[i]):\n",
    "                    return chr(ord(\"a\")+i)\n",
    "        message2=\"\"\n",
    "        for i in message:\n",
    "            x=func1(i)\n",
    "            message2=message2+x\n",
    "        return message2\n",
    "        \n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        mapper = {' ': ' '}\n",
    "        i = 0\n",
    "        for ch in key:\n",
    "            if ch not in mapper:\n",
    "                mapper[ch] = chr(ord('a') + i)\n",
    "                i += 1\n",
    "        return ''.join(mapper[ch] for ch in message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic = {}\n",
    "        for ch in key:\n",
    "            if ch!=\" \" and ch not in dic:\n",
    "                dic[ch] = len(dic)\n",
    "        \n",
    "        ans = \"\"\n",
    "        for ch in message:\n",
    "            if ch.isalpha():\n",
    "                ans += chr( dic[ch] + ord(\"a\") )\n",
    "            else:\n",
    "                ans += ch \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dict0 = dict()\n",
    "        res = ''\n",
    "        dict0[' '] = ' '\n",
    "        key0 = key.replace(' ', '')\n",
    "        # print(key0)\n",
    "        char_now = 'a'\n",
    "        dict0[key0[0]] = char_now\n",
    "        for i in range(len(key0)):\n",
    "            if key0[i] not in dict0:\n",
    "                char_now = chr(ord(char_now) + 1)\n",
    "                dict0[key0[i]] = char_now\n",
    "        # print(dict0)\n",
    "        for m in message:\n",
    "            res += dict0[m]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        key_dict = {' ':' '}\n",
    "        char = 'a'\n",
    "        # 获取key首字母排序并使key_list与字母表组成字典\n",
    "        for i in range(len(key)):\n",
    "            if key[i] != ' ' and (key[i] not in key[0:i]):\n",
    "                key_dict[key[i]] = char\n",
    "                char = chr(ord(char)+1)\n",
    "        ret = []\n",
    "        for i in message:\n",
    "            ret += key_dict[i]\n",
    "        return ''.join(ret)\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        table = {}\n",
    "        res = \"\"\n",
    "        for c in key:\n",
    "            if c == \" \" or c in table:\n",
    "                continue\n",
    "            \n",
    "            else:\n",
    "                table[c] = chr(ord('a') + len(table))\n",
    "\n",
    "        for c in message:\n",
    "            if c == \" \":\n",
    "                res += c\n",
    "            else:\n",
    "                res += table[c]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        # 构建替换字典\n",
    "        dic = dict()\n",
    "        count = 0\n",
    "        for i in range(0, len(key)):\n",
    "            if count == 26:\n",
    "                break\n",
    "            if key[i] == ' ':\n",
    "                continue\n",
    "            if key[i] not in dic:\n",
    "                dic[key[i]] = chr(ord('a') + count)\n",
    "                count += 1\n",
    "        # 执行替换\n",
    "        res = ''\n",
    "        for i in range(0, len(message)):\n",
    "            if message[i] == ' ':\n",
    "                res += ' '\n",
    "            else:\n",
    "                res += dic[message[i]]\n",
    "\n",
    "        # for i in range(0, len(dic)):\n",
    "        #     c = chr(ord('a') + i)\n",
    "        #     if c in dic:\n",
    "        #         message.replace(c, dic[c])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        hash_d = {' ':' '}\n",
    "        cnt = 0\n",
    "        res = ''\n",
    "        for i in key:\n",
    "            if i not in hash_d:\n",
    "                hash_d[i] = chr(ord('a') + cnt)\n",
    "                cnt += 1\n",
    "        for i in message:\n",
    "            res += hash_d[i]\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic ={}\n",
    "        res=[]\n",
    "        for i in key:\n",
    "            if i!=' ':\n",
    "                res.append(i)\n",
    "        index=0\n",
    "        for string in res:\n",
    "            if string==' ':\n",
    "                continue\n",
    "            elif string in dic:\n",
    "                continue\n",
    "            else:\n",
    "                dic[string]=chr(ord('a')+index)\n",
    "                index+=1\n",
    "        t=list(message)\n",
    "        for i in range(len(t)):\n",
    "            if i==' ':\n",
    "                continue\n",
    "            else:\n",
    "                if t[i] in dic:\n",
    "                    t[i]=dic[t[i]]\n",
    "        return ''.join(t) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        cur = \"a\"\n",
    "        rules = dict()\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        ans = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        alphabet = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        i = 0\n",
    "        d = {' ': ' '}\n",
    "        for k in key:\n",
    "            if k not in d:\n",
    "                d[k] = alphabet[i]\n",
    "                i+=1\n",
    "        return ''.join([d[i] for i in message])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        mp, curr = {}, 97\n",
    "        for c in key:\n",
    "            if c != ' ' and c not in mp:\n",
    "                mp[c] = chr(curr)\n",
    "                curr += 1\n",
    "        return \"\".join(mp.get(c, ' ') for c in message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        key=key.split()\n",
    "        key2=key3=\"\"\n",
    "        for i in key:\n",
    "            key2+=i\n",
    "        for i in key2:\n",
    "            if i not in key3:\n",
    "                key3+=i   \n",
    "        def func1(a:str)->str:\n",
    "            if a==\" \":\n",
    "                return \" \"\n",
    "            for i in range(26):\n",
    "                if ord(a)==ord(key3[i]):\n",
    "                    return chr(ord(\"a\")+i)\n",
    "        message2=\"\"\n",
    "        for i in message:\n",
    "            x=func1(i)\n",
    "            message2=message2+x\n",
    "        return message2\n",
    "        \n",
    "\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        stack = []\n",
    "        for i in key:\n",
    "            if i not in stack and i != ' ' and len(stack) < 26:\n",
    "                stack.append(i)\n",
    "        dic = {}\n",
    "        for j in range(0,26):\n",
    "            dic[stack[j]] = chr(97+j)\n",
    "        # return dic\n",
    "        res = ''\n",
    "        for m in message:\n",
    "            if m != ' ':\n",
    "                res += dic[m]\n",
    "            else:\n",
    "                res += ' '\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d = {\" \": \" \"}\n",
    "        i = 0\n",
    "        for c in key:\n",
    "            if c not in d:\n",
    "                d[c] = ascii_lowercase[i]\n",
    "                i += 1\n",
    "        return \"\".join(d[c] for c in message)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        output_list = []\n",
    "        \n",
    "        newkey=''\n",
    "        for char in key:\n",
    "            if char not in newkey:\n",
    "                newkey+=char\n",
    "        \n",
    "        qq=\"\".join(newkey.split(\" \"))\n",
    "        newqq=list(qq)\n",
    "        value=[\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]\n",
    "        dict1=dict(zip(newqq,value))\n",
    "        dict1[\" \"]=\" \"\n",
    "        newmessage=list(message)\n",
    "\n",
    "        for ww in newmessage:\n",
    "            output_list.append(dict1[ww]) \n",
    "        return \"\".join(output_list)\n",
    "\n",
    "\n",
    "        \n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        d = {}\n",
    "        i = 97\n",
    "        for c in key:\n",
    "            if c == \" \":\n",
    "                continue\n",
    "            if c not in d:\n",
    "                d[c] = chr(i) \n",
    "                i += 1\n",
    "        m = list(message)\n",
    "        for i, c in enumerate(m):\n",
    "            if c != \" \":\n",
    "                m[i] = d[c]\n",
    "        return \"\".join(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        dic={}\n",
    "        ans=set()\n",
    "        index=ord('a')\n",
    "        for k in key:\n",
    "            if k==\" \" or k in ans:\n",
    "                continue\n",
    "            dic[k]=chr(index)\n",
    "            ans.add(k)\n",
    "            index+=1\n",
    "        res=\"\"\n",
    "        for m in message:\n",
    "            res+=dic.get(m,' ')\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        cur = \"a\"\n",
    "        rules = dict()\n",
    "\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "\n",
    "        ans = \"\".join(rules.get(c, \" \") for c in message)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        char_dx = {i - 65: chr(i).lower() for i in range(ord('A'), ord('Z') + 1)}\n",
    "        msg_dic = {}\n",
    "        num = 0\n",
    "        for s in key:\n",
    "            if s in msg_dic or s == ' ':\n",
    "                continue\n",
    "            msg_dic[s] = char_dx[num]\n",
    "            num += 1\n",
    "\n",
    "        new_msg = ''\n",
    "        for s in message:\n",
    "            if s != ' ':\n",
    "                s = msg_dic[s]\n",
    "            new_msg += s\n",
    "\n",
    "        return new_msg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        fkey = []\n",
    "        key = key.replace(\" \",\"\")\n",
    "        for c in key:\n",
    "            if c not in fkey:\n",
    "                fkey.append(c)\n",
    "        codebooks = dict(zip(fkey,\"abcdefghijklmnopqrstuvwxyz\"))\n",
    "        decode =\"\"\n",
    "        print(codebooks)\n",
    "        for c in message:\n",
    "            if c == \" \":\n",
    "                decode += \" \"\n",
    "            else:\n",
    "                decode += codebooks[c]\n",
    "        return decode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        map_key = {' ': ' '}\n",
    "        cnt = 0\n",
    "        for i in range(len(key)):\n",
    "            char = key[i]\n",
    "            if char in map_key.keys() or not(ord('a') <= ord(char) <= ord('z')):\n",
    "                continue\n",
    "            map_key[char] = chr(ord('a') + cnt)\n",
    "            cnt += 1\n",
    "\n",
    "        # 第二种返回方式\n",
    "        return ''.join(map_key[i] for i in message)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        alpbt = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        keytrim = ''\n",
    "        for l in key:\n",
    "            if l not in keytrim and l != ' ':\n",
    "                keytrim += l\n",
    "        for l in alpbt:\n",
    "            if l not in keytrim:\n",
    "                keytrim += l\n",
    "        result = ''\n",
    "        for l in message:\n",
    "            if l == ' ':\n",
    "                result += ' '\n",
    "            else:\n",
    "                result += alpbt[keytrim.index(l)]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        origional = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        key_message = ''\n",
    "        dict = {}\n",
    "        for each_cha in key:\n",
    "            if each_cha == ' ':\n",
    "                continue\n",
    "            if each_cha not in key_message:\n",
    "                key_message += each_cha\n",
    "        for each_cha in key_message:\n",
    "            index_each = key_message.index(each_cha)\n",
    "            dict[each_cha] = origional[index_each]\n",
    "        result = ''\n",
    "        decode_cha = ''\n",
    "        for each_cha in message:\n",
    "            if each_cha == ' ':\n",
    "                decode_cha = ' '\n",
    "            else:\n",
    "                decode_cha = dict[each_cha]\n",
    "            result += decode_cha\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        rules = dict()\n",
    "        cur = \"a\"\n",
    "        for c in key:\n",
    "            if c != \" \" and c not in rules:\n",
    "                rules[c] = cur\n",
    "                cur = chr(ord(cur) + 1)\n",
    "        ans =\"\".join(rules.get(c,\" \") for c in message)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def decodeMessage(self, key: str, message: str) -> str:\n",
    "        abc = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "        secrets = dict()\n",
    "        secrets[\" \"] = \" \"\n",
    "        i = 0\n",
    "        for k in key:\n",
    "            if k == \" \":\n",
    "                continue\n",
    "            if k not in secrets:\n",
    "                secrets[k] = abc[i]\n",
    "                i += 1\n",
    "            if i >= 26:\n",
    "                break\n",
    "        ans = []\n",
    "        for ch in message:\n",
    "            ans.append(secrets[ch])\n",
    "        return \"\".join(ans)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
